<?

require('fpdf/fpdf.php');

class PDF_Reporte extends FPDF {

// private variables
    var $columnas;
    var $format;
    var $angle = 0;

// private functions
    function RoundedRect($x, $y, $w, $h, $r, $style = '') {
        $k = $this->k;
        $hp = $this->h;
        if ($style == 'F')
            $op = 'f';
        elseif ($style == 'FD' || $style == 'DF')
            $op = 'B';
        else
            $op = 'S';
        $MyArc = 4 / 3 * (sqrt(2) - 1);
        $this->_out(sprintf('%.2F %.2F m', ($x + $r) * $k, ($hp - $y) * $k));
        $xc = $x + $w - $r;
        $yc = $y + $r;
        $this->_out(sprintf('%.2F %.2F l', $xc * $k, ($hp - $y) * $k));

        $this->_Arc($xc + $r * $MyArc, $yc - $r, $xc + $r, $yc - $r * $MyArc, $xc + $r, $yc);
        $xc = $x + $w - $r;
        $yc = $y + $h - $r;
        $this->_out(sprintf('%.2F %.2F l', ($x + $w) * $k, ($hp - $yc) * $k));
        $this->_Arc($xc + $r, $yc + $r * $MyArc, $xc + $r * $MyArc, $yc + $r, $xc, $yc + $r);
        $xc = $x + $r;
        $yc = $y + $h - $r;
        $this->_out(sprintf('%.2F %.2F l', $xc * $k, ($hp - ($y + $h)) * $k));
        $this->_Arc($xc - $r * $MyArc, $yc + $r, $xc - $r, $yc + $r * $MyArc, $xc - $r, $yc);
        $xc = $x + $r;
        $yc = $y + $r;
        $this->_out(sprintf('%.2F %.2F l', ($x) * $k, ($hp - $yc) * $k));
        $this->_Arc($xc - $r, $yc - $r * $MyArc, $xc - $r * $MyArc, $yc - $r, $xc, $yc - $r);
        $this->_out($op);
    }

    function _Arc($x1, $y1, $x2, $y2, $x3, $y3) {
        $h = $this->h;
        $this->_out(sprintf('%.2F %.2F %.2F %.2F %.2F %.2F c ', $x1 * $this->k, ($h - $y1) * $this->k, $x2 * $this->k, ($h - $y2) * $this->k, $x3 * $this->k, ($h - $y3) * $this->k));
    }

    function Rotate($angle, $x = -1, $y = -1) {
        if ($x == -1)
            $x = $this->x;
        if ($y == -1)
            $y = $this->y;
        if ($this->angle != 0)
            $this->_out('Q');
        $this->angle = $angle;
        if ($angle != 0) {
            $angle*=M_PI / 180;
            $c = cos($angle);
            $s = sin($angle);
            $cx = $x * $this->k;
            $cy = ($this->h - $y) * $this->k;
            $this->_out(sprintf('q %.5F %.5F %.5F %.5F %.2F %.2F cm 1 0 0 1 %.2F %.2F cm', $c, $s, -$s, $c, $cx, $cy, -$cx, -$cy));
        }
    }

    function _endpage() {
        if ($this->angle != 0) {
            $this->angle = 0;
            $this->_out('Q');
        }
        parent::_endpage();
    }

// public functions
    function sizeOfText($texte, $largeur) {
        $index = 0;
        $nb_lines = 0;
        $loop = TRUE;
        while ($loop) {
            $pos = strpos($texte, "\n");
            if (!$pos) {
                $loop = FALSE;
                $ligne = $texte;
            } else {
                $ligne = substr($texte, $index, $pos);
                $texte = substr($texte, $pos + 1);
            }
            $length = floor($this->GetStringWidth($ligne));
            if ($largeur != 0)
                $res = 1 + floor($length / $largeur);
            else
                $res = 1;

            $nb_lines += $res;
        }
        return $nb_lines;
    }

    function addMembrete($nom, $adresse) {
        $this->SetFont('Arial', 'B', 11);
        $length = $this->GetStringWidth($nom) + 5;
        $this->Cell($length, 15, $nom);
        //$this->SetXY( $x1, $y1 + 8 );
        $this->SetFont('Arial', '', 10);

        $length = $this->GetStringWidth($adresse) + 10;
        $lignes = $this->sizeOfText($adresse, $length);
        $this->MultiCell($length, 10, $adresse);
        //$r1  = $this->w - 70;
        $r1 = 75;
        $r2 = $r1 + 130;
        $y1 = 8; // 8
        $y2 = $y1 + 30; //+12


        $this->SetLineWidth(0.1);
        //$this->SetFillColor(192);
        $this->RoundedRect($r1, $y1, ($r2 - $r1), $y2, 2.5, 'D');
        //$this->SetXY( $r1 + ($r2-$r1)/2 - 5, $y1+3 );
        $this->SetXY($r1 + 10, $y1 + 10);
    }

    function addLogo() {
        $x1 = 12;
        $r1 = 10;
        $r2 = $r1 + 60;
        $y1 = 8; // 8
        $y2 = $y1 + 38;
        $mid = $y1 + (($y2 - $y1) / 2);
        $this->RoundedRect($r1, $y1, ($r2 - $r1), ($y2 - $y1), 2.5, 'D');
        $this->SetXY($x1, $y1 + 4); //$x1
        $this->SetFont('Arial', 'B', 11);
        $this->SetXY($x1, $y1 + 8);
        $this->SetFont('Arial', '', 10);
        $this->Image('images/logoMPD.png', 12, 10, 55, 25);
    }

    function addEscudo() {

        $this->SetXY(130, 10);
        $this->Image('images/escudo.png', 100, 0, 20, 30);
    }

    function addDate($date) {
        // $r1  = $this->w - 200;
        $r1 = $this->w - 200;
        $r2 = $r1 + 30;
        $y1 = 39;
        $y2 = $y1 + 10;
        $mid = $y1 + (($y2 - $y1) / 2);
        $this->RoundedRect($r1, $y1, ($r2 - $r1), ($y2 - $y1), 2.5, 'D');
        $this->Line($r1, $mid, $r2, $mid);
        $this->SetXY($r1 + ($r2 - $r1) / 2 - 5, $y1 + 1);
        $this->SetFont("Arial", "B", 10);
        $this->Cell(10, 4, "Fecha", 0, 0, "C");
        $this->SetXY($r1 + ($r2 - $r1) / 2 - 5, $y1 + 5);
        $this->SetFont("Arial", "", 10);
        $this->Cell(10, 5, $date, 0, 0, "C");
    }

    function addPageNumber($page) {
        $r1 = $this->w - 80;
        $r2 = $r1 + 19;
        $y1 = 17;
        $y2 = $y1;
        $mid = $y1 + ($y2 / 2);
        $this->RoundedRect($r1, $y1, ($r2 - $r1), $y2, 3.5, 'D');
        $this->Line($r1, $mid, $r2, $mid);
        $this->SetXY($r1 + ($r2 - $r1) / 2 - 5, $y1 + 3);
        $this->SetFont("Arial", "B", 10);
        $this->Cell(10, 5, "PAGE", 0, 0, "C");
        $this->SetXY($r1 + ($r2 - $r1) / 2 - 5, $y1 + 9);
        $this->SetFont("Arial", "", 10);
        $this->Cell(10, 5, $page, 0, 0, "C");
    }

    function addCols($tab) {
        global $columnas;

        $r1 = 10; 
        $r2 = $this->w - ($r1 * 2);
        $y1 = 60; //45
        $y2 = $this->h - 10 - $y1;

        $this->SetXY($r1, $y1);
        $this->Rect($r1, $y1, $r2, $y2, "D");//margen izquierdo, margen derecho, ancho, largo,?..
        $this->Line($r1, $y1 + 6, $r1 + $r2, $y1 + 6);//linea debajo de los titulos de la tabla
        $colX = $r1;
        $columnas = $tab;

        while (list( $lib, $pos ) = each($tab)) {
            $this->SetXY($colX, $y1 + 2);
            $this->Cell($pos, 1, $lib, 0, 0, "C");
            $colX += $pos;
            $this->Line($colX, $y1, $colX, $y1 + $y2);
        }
    }

    function addLineFormat($tab) {
        global $format, $columnas;

        while (list( $lib, $pos ) = each($columnas)) {
            if (isset($tab["$lib"]))
                $format[$lib] = $tab["$lib"];
        }
    }

    function lineVert($tab) {
        global $columnas;
        reset($columnas);
        $maxSize = 0;

        while (list( $lib, $pos ) = each($columnas)) {
            $texte = $tab[$lib];
            $longCell = $pos - 2;
            $size = $this->sizeOfText($texte, $longCell);
            if ($size > $maxSize)
                $maxSize = $size;
        }
        return $maxSize;
    }

    function addLine($posY, $tab) {
        global $columnas, $format;

        $posX = 10;
        $maxSize = $posY;

        reset($columnas);

        while (list( $lib, $pos ) = each($columnas)) {
            $longCell = $pos - 2;
            $texte = $tab[$lib];
            $length = $this->GetStringWidth($texte);
            $tailleTexte = $this->sizeOfText($texte, $length);
            $formText = $format[$lib];
            $this->SetXY($posX, $posY - 1);
            $this->MultiCell($longCell, 4, $texte, 0, $formText);
            if ($maxSize < ($this->GetY() ))
                $maxSize = $this->GetY();
            $posX += $pos;
        }
        return ( $maxSize - $posY );
    }

    function addPiePreview($txt) {
        $this->SetFont("Arial", "", 9);
        //$this->Rect(10, 250, 190, 20);
        $this->Text(70, 260, $txt);
    }

    function addTexto($txt) {
        $this->SetFont("Arial", "", 9);
//$this->Rect(10,200,190,20);
        $this->Text(10, 55, $txt);
    }

    function txtVertical($texte) {
        $this->SetFont('Arial', 'B', 9);
        //  $this->SetTextColor(192,192,192);
        $this->Rotate(90, 7, 260);
        $this->Text(7, 260, $texte);
        $this->Rotate(0);
        $this->SetTextColor(0, 0, 0);
    }

    /*     * ************************************************************************************* */
    /* Para Caratula de Expediente */

    function addEncabezado($txt) {
        $this->SetFont("Arial", "", 15);
        //$this->Rect(10,200,190,20);
        $this->Text(15, 25, $txt);
    }

}

?>